package com.hy.ai.tool;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.hy.ai.entity.po.Products;
import com.hy.ai.entity.po.Purchases;
import com.hy.ai.entity.po.UserPreferences;
import com.hy.ai.entity.query.ProduceQuery;
import com.hy.ai.entity.query.UserPreferencesQuery;
import com.hy.ai.service.IProductsService;
import com.hy.ai.service.IPurchasesService;
import com.hy.ai.service.IUserPreferencesService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
@RequiredArgsConstructor
@Component
public class ProductsTools {
    private final IProductsService productsService;
    private final IPurchasesService purchasesService;
    private final IUserPreferencesService userPreferencesService;

    @Tool(description = "查询商品信息")
    public List<Products> queryProducts(@ToolParam(description = "查询的条件",required = false) ProduceQuery query){
        if(query==null){
            return productsService.list();
        }
        QueryChainWrapper<Products> wrapper = productsService.query().
                eq(query.getCategory() != null, "category", query.getCategory());
        if(query.getSorts()!=null&& !query.getSorts().isEmpty()){
            for (ProduceQuery.sort sort : query.getSorts()) {
                wrapper.orderBy(true, sort.getAsc(), sort.getFiled());
            }
        }
        return wrapper.list();
    }
    @Tool(description = "查询被赠送人的姓名")
    public UserPreferences queryUser(@ToolParam(description = "查询的条件") UserPreferencesQuery query){
        QueryWrapper<UserPreferences> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<UserPreferences> wrapper1 = wrapper.lambda().eq(UserPreferences::getUserName, query.getUserName());
        return userPreferencesService.getOne(wrapper1);
    }
    @Tool(description = "生成购买信息单,返回购买的单号")
    public Integer save(@ToolParam(description = "购买者的姓名") String buyerName,
                        @ToolParam(description = "购买者的电话") String phone,
                        @ToolParam(description = "购买的商品id") Integer productId){
        Purchases purchases = new Purchases();
        purchases.setBuyerName(buyerName).setPhone(phone)
                .setProductId(productId).setPurchaseTime(LocalDateTime.now());
        purchasesService.save(purchases);
        return purchases.getId();
    }
}
